package com.haogu.controller;

import com.haogu.entity.Dict;
import com.haogu.entity.DictType;
import com.haogu.entity.User;
import com.haogu.service.DictService;
import com.haogu.service.DictTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("/dict")
public class DictController {

    @Resource
    private DictTypeService dictTypeService;

    @Resource
    private DictService dictService;


    /**
     * 去index_dict.html页面
     * @return
     */
    @RequestMapping("/toIndexDict")
    public String toIndexDict(){
        return "dict/index_dict";
    }


    //-----------------以下为字典类型--------------------------------------------
    /**
     * 去添加字典类型页面
     */
    @RequestMapping("/toAddDictType")
    public String toAddDictType(){
        return "dict/add_dictType";
    }
    @RequestMapping("/addDictType")
    @ResponseBody
    public Map addDictType(DictType dictType, HttpSession session){

        return dictTypeService.addDictType(dictType,session);
    }
    /**
     *字典类型的删除
     */
    @RequestMapping("/deleteDictType")
    @ResponseBody
    public Map deleteDictType(Integer id){
        return dictTypeService.delectDictType(id);
    }
    /**
     * 去修改字典类型页面
     */
    @RequestMapping("/toUpdateDictType")
    public String toEditDictType(Integer id, Model model){
        dictTypeService.toUpdateDictType(id,model);
        return "dict/edit_dictType";
    }

    @RequestMapping("/updateDictType")
    @ResponseBody
    public Map updateDictType(DictType dictType,HttpSession session){

        return dictTypeService.updateDictType(dictType,(User)session.getAttribute("user"));
    }
    /**
     * 展示字典类型
     */
    @RequestMapping("/listDictType")
    @ResponseBody
    public Map listDictType(){
        return  dictTypeService.selectDictTypeByPage();
    }


    //-----------------以下为字典-------------------------------------------------
    /**
     * 去添加字典页面
     */
    @RequestMapping("/toAddDict")
    public String toAddDict(Model model){
        dictService.toAddDict(model);
        return "dict/add_dict";
    }
    @RequestMapping("/addDict")
    @ResponseBody
    public Map addDict(Dict dict){
        return dictService.addDict(dict);
    }
    /**
     *字典的删除
     */
    @RequestMapping("/deleteDict")
    @ResponseBody
    public Map deleteDict(String ids){

        return dictService.deleteDict(ids);
    }
    /**
     * 去修改字典页面
     */
    @RequestMapping("/toUpdateDict")
    public String toEditDict(Integer id,Model model){
        dictService.toUpdateDict(id,model);
        return "dict/edit_dict";
    }
    @RequestMapping("/updateDict")
    @ResponseBody
    public Map updateDict(Dict dict){
        return dictService.updateDict(dict);
    }
    /**
     * 展示字典
     */
    @RequestMapping("/listDict")
    @ResponseBody
    public Map listDict(Integer page,Integer rows,Integer dictTypeId){

        return dictService.selectDictByPage(page,rows,dictTypeId);
    }
}
